"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _jsxRuntime = require("react/jsx-runtime");

var _proProvider = require("@ant-design/pro-provider");

var _antdV = require("antd-v4");

var _omit = _interopRequireDefault(require("omit.js"));

var _useMergedState3 = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));

var _react = _interopRequireWildcard(require("react"));

var _excluded = ["content", "numberFormatOptions", "numberPopoverRender"],
    _excluded2 = ["text", "mode", "render", "renderFormItem", "fieldProps", "proFieldKey", "plain", "valueEnum", "placeholder", "locale", "customSymbol", "numberFormatOptions", "numberPopoverRender"];
var defaultMoneyIntl = new Intl.NumberFormat('zh-Hans-CN', {
  currency: 'CNY',
  style: 'currency'
});
var enMoneyIntl = {
  style: 'currency',
  currency: 'USD'
};
var ruMoneyIntl = {
  style: 'currency',
  currency: 'RUB'
};
var rsMoneyIntl = {
  style: 'currency',
  currency: 'RSD'
};
var msMoneyIntl = {
  style: 'currency',
  currency: 'MYR'
};
var ptMoneyIntl = {
  style: 'currency',
  currency: 'BRL'
};
var intlMap = {
  default: defaultMoneyIntl,
  'zh-Hans-CN': {
    currency: 'CNY',
    style: 'currency'
  },
  'en-US': enMoneyIntl,
  'ru-RU': ruMoneyIntl,
  'ms-MY': msMoneyIntl,
  'sr-RS': rsMoneyIntl,
  'pt-BR': ptMoneyIntl
};

var getTextByLocale = function getTextByLocale(localeStr, paramsText, precision, config) {
  var moneyText = paramsText;

  if (typeof moneyText === 'string') {
    moneyText = Number(moneyText);
  }

  if (!moneyText && moneyText !== 0) return '';
  return new Intl.NumberFormat(localeStr || 'zh-Hans-CN', (0, _objectSpread2.default)((0, _objectSpread2.default)({}, intlMap[localeStr || 'zh-Hans-CN'] || intlMap['zh-Hans-CN']), {}, {
    maximumFractionDigits: precision
  }, config)).format(moneyText);
};

var DefaultPrecisionCont = 2;

var InputNumberPopover = /*#__PURE__*/_react.default.forwardRef(function (_ref, ref) {
  var content = _ref.content,
      numberFormatOptions = _ref.numberFormatOptions,
      numberPopoverRender = _ref.numberPopoverRender,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useMergedState = (0, _useMergedState3.default)(function () {
    return rest.defaultValue;
  }, {
    value: rest.value,
    onChange: rest.onChange
  }),
      _useMergedState2 = (0, _slicedToArray2.default)(_useMergedState, 2),
      value = _useMergedState2[0],
      onChange = _useMergedState2[1];

  var dom = content === null || content === void 0 ? void 0 : content((0, _objectSpread2.default)((0, _objectSpread2.default)({}, rest), {}, {
    value: value
  }));
  var props = {
    visible: dom ? rest.visible : false
  };
  return (0, _jsxRuntime.jsx)(_antdV.Popover, (0, _objectSpread2.default)((0, _objectSpread2.default)({
    placement: "topLeft"
  }, props), {}, {
    trigger: ['focus', 'click'],
    content: dom,
    getPopupContainer: function getPopupContainer(triggerNode) {
      return (triggerNode === null || triggerNode === void 0 ? void 0 : triggerNode.parentElement) || document.body;
    },
    children: (0, _jsxRuntime.jsx)(_antdV.InputNumber, (0, _objectSpread2.default)((0, _objectSpread2.default)({
      ref: ref
    }, rest), {}, {
      value: value,
      onChange: onChange
    }))
  }));
});
/**
 * 金额组件
 *
 * @param FieldMoneyProps {
 *     text: number;
 *     moneySymbol?: string; }
 */


var FieldMoney = function FieldMoney(_ref2, ref) {
  var _fieldProps$customSym, _fieldProps$precision;

  var text = _ref2.text,
      type = _ref2.mode,
      render = _ref2.render,
      renderFormItem = _ref2.renderFormItem,
      fieldProps = _ref2.fieldProps,
      proFieldKey = _ref2.proFieldKey,
      plain = _ref2.plain,
      valueEnum = _ref2.valueEnum,
      placeholder = _ref2.placeholder,
      _ref2$locale = _ref2.locale,
      locale = _ref2$locale === void 0 ? (_fieldProps$customSym = fieldProps.customSymbol) !== null && _fieldProps$customSym !== void 0 ? _fieldProps$customSym : 'zh-Hans-CN' : _ref2$locale,
      _ref2$customSymbol = _ref2.customSymbol,
      customSymbol = _ref2$customSymbol === void 0 ? fieldProps.customSymbol : _ref2$customSymbol,
      _ref2$numberFormatOpt = _ref2.numberFormatOptions,
      numberFormatOptions = _ref2$numberFormatOpt === void 0 ? fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.numberFormatOptions : _ref2$numberFormatOpt,
      _ref2$numberPopoverRe = _ref2.numberPopoverRender,
      numberPopoverRender = _ref2$numberPopoverRe === void 0 ? (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.numberPopoverRender) || false : _ref2$numberPopoverRe,
      rest = (0, _objectWithoutProperties2.default)(_ref2, _excluded2);
  var precision = (_fieldProps$precision = fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.precision) !== null && _fieldProps$precision !== void 0 ? _fieldProps$precision : DefaultPrecisionCont;
  var intl = (0, _proProvider.useIntl)(); // 当手动传入locale时，应该以传入的locale为准，未传入时则根据全局的locale进行国际化

  if (locale && _proProvider.intlMap[locale]) {
    intl = _proProvider.intlMap[locale];
  }

  var moneySymbol = (0, _react.useMemo)(function () {
    if (customSymbol) {
      return customSymbol;
    }

    var defaultText = intl.getMessage('moneySymbol', '￥');

    if (rest.moneySymbol === false || fieldProps.moneySymbol === false) {
      return undefined;
    }

    return defaultText;
  }, [customSymbol, fieldProps.moneySymbol, intl, rest.moneySymbol]);

  if (type === 'read') {
    var dom = (0, _jsxRuntime.jsx)("span", {
      ref: ref,
      children: getTextByLocale(moneySymbol ? locale : false, text, precision, numberFormatOptions !== null && numberFormatOptions !== void 0 ? numberFormatOptions : fieldProps.numberFormatOptions)
    });

    if (render) {
      return render(text, (0, _objectSpread2.default)({
        mode: type
      }, fieldProps), dom);
    }

    return dom;
  }

  if (type === 'edit' || type === 'update') {
    var _dom = (0, _jsxRuntime.jsx)(InputNumberPopover, (0, _objectSpread2.default)({
      content: function content(props) {
        var _props$value, _props$value$toString;

        if (numberPopoverRender === false) return;
        if (!props.value) return;
        var reg = new RegExp("/B(?=(d{".concat(3 + (precision - DefaultPrecisionCont), "})+(?!d))/g"));
        var localeText = getTextByLocale(moneySymbol ? locale : false, (_props$value = props.value) === null || _props$value === void 0 ? void 0 : (_props$value$toString = _props$value.toString()) === null || _props$value$toString === void 0 ? void 0 : _props$value$toString.replace(reg, ','), precision, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, numberFormatOptions), {}, {
          notation: 'compact'
        }));

        if (typeof numberPopoverRender === 'function') {
          return numberPopoverRender === null || numberPopoverRender === void 0 ? void 0 : numberPopoverRender(props, localeText);
        }

        return localeText;
      },
      ref: ref,
      precision: precision,
      // 删除默认min={0}，允许输入一个负数的金额，用户可自行配置min来限制是否允许小于0的金额
      formatter: function formatter(value) {
        if (value && moneySymbol) {
          var reg = new RegExp("/B(?=(d{".concat(3 + (precision - DefaultPrecisionCont), "})+(?!d))/g"));
          return "".concat(moneySymbol, " ").concat(value).replace(reg, ',');
        }

        return value === null || value === void 0 ? void 0 : value.toString();
      },
      parser: function parser(value) {
        if (moneySymbol && value) {
          return value.replace(new RegExp("\\".concat(moneySymbol, "\\s?|(,*)"), 'g'), '');
        }

        return value;
      },
      placeholder: placeholder
    }, (0, _omit.default)(fieldProps, ['numberFormatOptions', 'precision', 'numberPopoverRender', 'customSymbol'])));

    if (renderFormItem) {
      return renderFormItem(text, (0, _objectSpread2.default)({
        mode: type
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

var _default = /*#__PURE__*/_react.default.forwardRef(FieldMoney);

exports.default = _default;